package com.example.SocketSelectorStudy;

import com.sun.org.apache.bcel.internal.generic.InstructionConstants;
import org.aspectj.weaver.SignatureUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by Administrator on 2017/5/24.
 */
public class SelectorServer {

    public static ByteBuffer sendBuffer = ByteBuffer.allocate(1024);
    public static ByteBuffer receiveBuffer = ByteBuffer.allocate(1024);
    public static void main(String[] args) throws IOException {

        //创建服务 绑定端口
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.bind(new InetSocketAddress(8888));

        //创建选择器
        Selector selector = Selector.open();

        //绑定服务到选择器上
        serverSocketChannel.configureBlocking(false);//false表示非阻塞模式,true表示阻塞模式
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        //监听端口
        while (true) {
            int readyChannel = selector.select();
            if (readyChannel == 0) continue;
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey selectionKey = iterator.next();
                dealSelectKey(selector, selectionKey);
                iterator.remove();
            }

        }







    }

    private static void dealSelectKey(Selector selector, SelectionKey selectionKey) throws IOException {
        if (selectionKey.isAcceptable()) {
            ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
            SocketChannel clientSocketChannel = serverSocketChannel.accept();
            clientSocketChannel.configureBlocking(false);
            clientSocketChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
            System.out.println("channel is ready acceptable.");
        } else if (selectionKey.isConnectable()) {
            selectionKey.channel().register(selector, SelectionKey.OP_READ);
            System.out.println("channel is connectable.");
        }else if(selectionKey.isReadable()){
            SocketChannel clientSocketChannel = (SocketChannel) selectionKey.channel();
            receiveBuffer.clear();
            clientSocketChannel.read(receiveBuffer);
            selectionKey.interestOps(SelectionKey.OP_WRITE);
            System.out.println("message from client is: " + new String(receiveBuffer.array()));
            System.out.println("Thread id : " + Thread.currentThread().getId());
        } else if(selectionKey.isWritable()){
            SocketChannel clientSocketChannel = (SocketChannel) selectionKey.channel();
            sendBuffer.flip();
            sendBuffer = ByteBuffer.wrap("zhuqianli".getBytes());
            System.out.println("sendBuffer = " + new String(sendBuffer.array()));
            clientSocketChannel.write(sendBuffer);
            selectionKey.interestOps(SelectionKey.OP_READ);
            System.out.println("channel is writable.");
        }


    }


}
